package com.security.spring.service;

import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @author Administrator
 */
@Component
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

//        Set<String> authritiesSet = new HashSet<>(userDO.getPermissions().size() + userDO.getRoles().size());

//        authritiesSet.addAll(userDO.getPermissions());
        // 这里需要注意，security里面角色和权限都是存在一个字段里面的，但是其角色会自动加上ROLE_前缀来将角色和权限进行区分，
        // 以便在进行判断是否有某角色时可以进行判断，但是我们一般使用是可以不用加前缀的，如@PreAuthorize("hasRole('role2')")，但是你加也没问题
//        authritiesSet.addAll(userDO.getRoles().stream().map(role -> "ROLE_" + role).collect(Collectors.toList()));

        UserDetails userDetails = User.withUsername("llh")
                .password("836f6d97-ccae-48df-b334-49c1f320623a")
                .build();
        return userDetails;
    }

}
